<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class login extends CI_Controller {

	public function __construct(){
		parent::__construct();
	}

	public function index(){
		if($this->session->userdata('logged_in')){
			redirect('registro', 'refresh');
		}else{
			$this->load->view('login_view');
		}
	}

	public function log(){
		if( $this->input->post() ){
			$this->load->model('login_model');
			$this->load->library('usuario');

			$usuario = new usuario();

			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$usuario->setUsu_username($username);
			$usuario->setUsu_password(md5($password));

			$usuario = $this->login_model->login($usuario);

			if( $usuario->getIsLogged() ){
				$datos = array(
					'usu_id'=>$usuario->getUsu_id(),
					'usu_username'=>$usuario->getUsu_username(),
					'usu_email'=>$usuario->getUsu_email(),
					'logged_in'=>true
				);
				$this->session->set_userdata($datos);
				redirect('/stock', 'refresh');				
			}else{
				$data['error']='Usuario/Email o Contraseña no validos';
				$this->load->view('login_view', $data);
			}
		}else{
			$this->load->view('login_view');
		}
	}

	public function cerrar(){
		$this->session->sess_destroy();
		redirect('', 'refresh');
	}
}

/* End of file  */
/* Location: ./application/controllers/ */